
package com.king.headline.util;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.text.Selection;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.king.headline.R;
import com.king.headline.adapter.AdapterDialogSelectTxt;
import com.king.headline.beans.UserInfo;
import com.king.headline.net.Callback;
import com.king.headline.net.NetWork;
import com.king.headline.view.RoundRectDradable;

import org.json.JSONObject;

import java.util.List;

public class DialogUtils {


    private static Object tag;

    public static interface OnDialogListener {
        public void onOk(View v);

        public void onCancel(View v);
    }


    public static interface OnItemSelectListener {
        public void onItemSelected(AdapterDialogSelectTxt.Item item);
    }


    private static void showDialog(Dialog dialog) {
        try {
            if (dialog != null && !dialog.isShowing())
                dialog.show();
        } catch (Exception e) {
        }
    }

    public static void showHintDialogOne(Context context, String textHint, final OnDialogListener listener) {
        showHintDialogOne(context, "我知道了", "提示", textHint, true, listener);
    }

    public static void showHintDialogOne(Context context, String textHint, boolean isCancelAble, final OnDialogListener listener) {
        showHintDialogOne(context, "我知道了", "提示", textHint, isCancelAble, listener);
    }

    public static void showHintDialogOne(Context context, String textHint) {
        showHintDialogOne(context, "我知道了", "提示", textHint, true, null);
    }

    public static void showHintDialogOne(Context context, String textBtnOk, String textTitle,
                                         String textHint, boolean isCancelAble, final OnDialogListener listener) {
        final Dialog dialog = new Dialog(context, R.style.dialog);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_hint_btn_ok, null);
        TextView title = (TextView) view.findViewById(R.id.title);
        TextView textContent = (TextView) view.findViewById(R.id.dialog_text_content);
        TextView btnOk = (TextView) view.findViewById(R.id.dialog_btn_ok);
        btnOk.setText(textBtnOk);
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
                if (listener != null) {
                    listener.onOk(v);
                }
            }
        });
        title.setText(textTitle);
        textContent.setText(textHint);
        dialog.setContentView(view);
        dialog.setCancelable(isCancelAble);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setBackgroundDrawable(new ColorDrawable(0));
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = Utils.getScreenWidth() * 13 / 16;
        dialogWindow.setAttributes(lp);
        DialogUtils.showDialog(dialog);
    }

    public static void showHintDialogTwo(Context context, String textHint, OnDialogListener listener) {

        showHintDialogTwo(context, "确定", "取消", "提示", textHint, true, listener);

    }

    public static void showHintDialogTwo(Context context, String textBtnOk, String textBtnCancel,
                                         String textTitle, String textHint, boolean isCancelAble, final OnDialogListener listener) {
        final Dialog dialog = new Dialog(context, R.style.dialog);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_hint_btn_two, null);
        TextView title = (TextView) view.findViewById(R.id.title);
        TextView textContent = (TextView) view.findViewById(R.id.dialog_text_content);
        TextView btnOk = (TextView) view.findViewById(R.id.dialog_btn_ok);
        TextView btnCancel = (TextView) view.findViewById(R.id.dialog_btn_cancel);
        btnOk.setText(textBtnOk);
        btnCancel.setText(textBtnCancel);

        RoundRectDradable rectDradable0 = new RoundRectDradable();
        rectDradable0.setColor(UIUtils.getColor(R.color.myred));
        rectDradable0.setRadius(0, 0, 0, UIUtils.dip2px(10));
        btnOk.setBackgroundDrawable(rectDradable0);
        RoundRectDradable rectDradable1 = new RoundRectDradable();
        rectDradable1.setColor(UIUtils.getColor(R.color.white));
        rectDradable1.setRadius(0, 0, UIUtils.dip2px(10), 0);
        btnCancel.setBackgroundDrawable(rectDradable1);

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
                if (listener != null) {
                    listener.onOk(v);
                }
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
                if (listener != null) {
                    listener.onCancel(v);
                }
            }
        });
        title.setText(textTitle);
        textContent.setText(textHint);
        dialog.setContentView(view);
        dialog.setCancelable(isCancelAble);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setBackgroundDrawable(new ColorDrawable(0));
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = Utils.getScreenWidth() * 13 / 16;
        dialogWindow.setAttributes(lp);

        DialogUtils.showDialog(dialog);
    }


    public static void showSelectDialog(Context context, String title, List<AdapterDialogSelectTxt.Item> list,
                                        final OnItemSelectListener itemClickListener) {
        final Dialog selectDialog = new Dialog(context, R.style.dialog);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_select, null);
        TextView titleView = (TextView) view.findViewById(R.id.title);
        titleView.setText(title);
        view.findViewById(R.id.dialog_btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectDialog != null && selectDialog.isShowing()) {
                    selectDialog.dismiss();
                }
            }
        });
        ListView listView = (ListView) view.findViewById(R.id.listview);
        final AdapterDialogSelectTxt adapter = new AdapterDialogSelectTxt(context, list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AdapterDialogSelectTxt.Item item = (AdapterDialogSelectTxt.Item) adapter.getItem(position);
                if (itemClickListener != null) {
                    itemClickListener.onItemSelected(item);
                }
                if (selectDialog != null && selectDialog.isShowing()) {
                    selectDialog.dismiss();
                }
            }
        });

        selectDialog.setContentView(view);
        Window dialogWindow = selectDialog.getWindow();
        dialogWindow.setBackgroundDrawable(new ColorDrawable(0));
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = Utils.getScreenWidth() * 13 / 16;
        dialogWindow.setAttributes(lp);
        DialogUtils.showDialog(selectDialog);
    }

    public static void showSingTextDialog(Context context, String title, String content) {
        final Dialog selectDialog = new Dialog(context, R.style.dialog);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_text, null);
        TextView titleView = (TextView) view.findViewById(R.id.title);
        titleView.setText(title);
        view.findViewById(R.id.dialog_btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectDialog != null && selectDialog.isShowing()) {
                    selectDialog.dismiss();
                }
            }
        });
        TextView tv_content = (TextView) view.findViewById(R.id.tv_content);
        tv_content.setText(content);
        selectDialog.setContentView(view);
        Window dialogWindow = selectDialog.getWindow();
        dialogWindow.setBackgroundDrawable(new ColorDrawable(0));
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = Utils.getScreenWidth() * 13 / 16;
        dialogWindow.setAttributes(lp);
        DialogUtils.showDialog(selectDialog);
    }

    public static void showModifyNickNameDialog(Context context, String oldNickName, final OnDialogListener listener) {
        final Dialog selectDialog = new Dialog(context, R.style.dialog);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_modify_nickname, null);
        TextView titleView = (TextView) view.findViewById(R.id.title);
        final EditText tv_content = (EditText) view.findViewById(R.id.tv_content);
        KeyBoardUtils.openKeybord(tv_content);
        View btnOk = (View) view.findViewById(R.id.bt_loging);
        titleView.setText("设置昵称");
        tv_content.setText(oldNickName);
        Editable etext = tv_content.getText();
        Selection.setSelection(etext, etext.length());
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = tv_content.getText().toString();
                if (!TextUtils.isEmpty(text)) {
                    v.setTag(text);
                    if (selectDialog != null && selectDialog.isShowing()) {
                        selectDialog.dismiss();
                        listener.onOk(v);
                    }
                } else {
                    CustomToast.showToast("昵称不能为空");
                }

            }
        });
        selectDialog.setContentView(view);
        Window dialogWindow = selectDialog.getWindow();
        dialogWindow.setBackgroundDrawable(new ColorDrawable(0));
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = Utils.getScreenWidth() * 13 / 16;
        dialogWindow.setAttributes(lp);
        DialogUtils.showDialog(selectDialog);
    }

    public static void setTag(Object tag) {
        DialogUtils.tag = tag;
    }

    public static void showModifyCommentDialog(final Context context, String hintText) {
        final Dialog selectDialog = new Dialog(context, R.style.dialog);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_modify_comment, null);
        final EditText tv_content = (EditText) view.findViewById(R.id.tv_content);
        View btnOk = (View) view.findViewById(R.id.bt_loging);
        tv_content.setHint(hintText);
        KeyBoardUtils.openKeybord(tv_content);
        btnOk.setTag(tag);
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tagString = (String) v.getTag();
                String sourceId;
                String replyID;
                if (tagString.contains("::::")) {
                    String[] tags = tagString.split("::::");
                    sourceId = tags[0];
                    replyID = tags[1];
                } else {
                    sourceId = tagString;
                    replyID = "";
                }
                String text = tv_content.getText().toString();
                KeyBoardUtils.closeKeybord(tv_content);
                comment(context,tv_content, sourceId, replyID);
                if (!TextUtils.isEmpty(text)) {
                    v.setTag(text);
                    if (selectDialog != null && selectDialog.isShowing()) {
                        selectDialog.dismiss();
                    }
                } else {
                    CustomToast.showToast("评论内容不能为空");
                }

            }
        });
        selectDialog.setContentView(view);
        Window dialogWindow = selectDialog.getWindow();
        dialogWindow.setBackgroundDrawable(new ColorDrawable(0));
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = Utils.getScreenWidth();
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialogWindow.setAttributes(lp);
        DialogUtils.showDialog(selectDialog);
    }


    /**
     * 评论
     */
    private static void comment(final Context context ,final EditText et_comment, String sourceId, String replyID) {
        UserInfo userInfo = AccountManager.getInstance().getUser();
        String text = et_comment.getText().toString();
        if (TextUtils.isEmpty(text)) {
            return;
        }

        NetWork.getInstance().comment(userInfo.encyptdId, userInfo.apikey, sourceId, replyID, text, new Callback() {
            @Override
            public void success(String result) {
                CustomToast.showToast("评论成功");
                et_comment.setText("");
            }

            @Override
            public void notice(JSONObject jsonObject, String message) {
                CustomToast.showToast(message);
            }

            @Override
            public void failed(String e) {
                CustomToast.showToast(e);
            }
        });
    }

}
